package cn.z2huo.mybatis.generator.mbp;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * <p>
 *
 * @author z2huo
 */
public class GeneratorTest {

    private static final String URL = "jdbc:mysql://localhost:3300/demo";

    private static final String USERNAME = "z2huo";

    private static final String PASSWORD = "z2huo@2024";

    private static final String OUTPUT_DIR = "generator/generator-mbp/src/main/java/cn/z2huo/mybatis/generator/mbp";

    public static void main(String[] args) {
        FastAutoGenerator
                .create(URL, USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author("z2huo")
                            .enableSwagger()
                            .outputDir(OUTPUT_DIR);
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        // 设置父包名
                        builder.parent("cn.z2huo.mybatis.generator.mbp")
                                // 设置父包模块名
                                .moduleName("system")
                                // 设置 mapper Xml 生成路径
                                .pathInfo(Collections.singletonMap(OutputFile.xml, OUTPUT_DIR))
                )
                .strategyConfig(builder -> builder
                        .addInclude("z2huo_user")
                        .addInclude("z2huo_role")
                        .addInclude("z2huo_system")
                        .entityBuilder()
                        // 启用 lombok
                        .enableLombok()
                        // 启用字段注释
                        .enableTableFieldAnnotation()
                )
                // 使用 Freemarker 引擎模板，默认的是 Velocity 引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}
